package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.common.result.R;
import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.user.util.AuthContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-03-14
 */
//就诊人管理接口
@RestController
@RequestMapping("/api/userinfo/patient")
public class PatientController {


    @Autowired
    private PatientService patientService;

    //添加就诊人
    @PostMapping("auth/save")
    public R  addPatient(@RequestBody Patient patient, HttpServletRequest request){
        patient.setUserId(AuthContextHolder.getUserId(request));
        patientService.save(patient);
        return R.ok();
    }

    //删除
    @DeleteMapping("/delete/{pid}")
    public R deletePatientById(@PathVariable Long pid){
        patientService.removeById(pid);
        return R.ok();
    }
    //修改：根据patientid查询就诊人信息
    @GetMapping("/info/{pid}")
    public R getPatientInfo(@PathVariable Long pid){
        Patient patient=patientService.getPatientInfo(pid);
        return R.ok().data("patient",patient);
    }

    //修改：
    @PutMapping("/update")
    public R updatePatient(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }
    //查询：查询当前登录用户底下所有的就诊人信息
    @GetMapping("/list")
    public R getPatientList(HttpServletRequest request){
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patientList= patientService.getPatientList(userId);
        return R.ok().data("list",patientList);
    }

    //根据就诊人id获取就诊人信息:给service_order模块远程调用使用的
    @GetMapping("inner/get/{id}")
    public Patient getPatientById(@PathVariable(value = "id") Long  id){
        return patientService.getById(id);
    }

}

